from django.db import models
from social.models import UserInform
from django.core.validators import MinValueValidator, MaxValueValidator


class MusicGenre(models.Model):
    """
    音乐类型表，用于存储音乐的不同类型或分类。
    """
    genre_name = models.CharField(max_length=100, verbose_name='类型名称')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '音乐类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.genre_name


class MusicDetails(models.Model):
    """
    音乐详情表，存储具体音乐的详细信息，包括音乐名称、艺术家、封面图片等。
    """
    music_name = models.CharField(max_length=100, verbose_name='音乐名称')
    artist = models.CharField(max_length=100, verbose_name='艺术家')
    cover_image = models.ImageField(
        upload_to='music/covers', default='default_music_img.png', max_length=300, verbose_name='封面图片')
    music_file = models.FileField(upload_to='music/mp3', verbose_name='音乐文件')
    play_count = models.IntegerField(default=0, verbose_name='播放次数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    lyrics = models.TextField(verbose_name='歌词')
    genre = models.ForeignKey(
        'MusicGenre', on_delete=models.CASCADE, verbose_name='音乐类型')
    # 评分字段，限制在0到5之间的一位小数
    # rating = models.FloatField(
    #     default=1.0,  # 默认值可以是0.0或任何你需要的默认评分
    #     validators=[MinValueValidator(0.0), MaxValueValidator(5.0)],
    #     verbose_name='评分'
    # )

    class Meta:
        verbose_name = '音乐详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.music_name


class UserFavoriteMusic(models.Model):
    """
    用户喜好音乐表，记录用户喜欢的音乐，用于个性化推荐和显示用户偏好。
    """
    user = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, verbose_name='用户')
    music = models.ForeignKey(
        'MusicDetails', on_delete=models.CASCADE, verbose_name='音乐')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户喜好音乐'
        verbose_name_plural = verbose_name


class HotMusic(models.Model):
    """
    热门音乐表，用于存储当前流行或热门的音乐。
    """
    music = models.ForeignKey(
        'MusicDetails', on_delete=models.CASCADE, verbose_name='音乐')
    rank = models.IntegerField(verbose_name='排名')
    average_rating = models.FloatField(verbose_name='平均评分')

    class Meta:
        verbose_name = '热门音乐'
        verbose_name_plural = verbose_name


class MusicComment(models.Model):
    """
    音乐评论表，用于存储用户对音乐的评论。
    """
    user = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, verbose_name='用户')
    music = models.ForeignKey(
        'MusicDetails', on_delete=models.CASCADE, verbose_name='音乐')
    comment_content = models.TextField(verbose_name='评论内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')

    class Meta:
        verbose_name = '音乐评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.user.username


class MusicRating(models.Model):
    """
    音乐评分表，允许用户对音乐进行评分，用于反映音乐的受欢迎程度。
    """
    user = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, verbose_name='用户')
    music = models.ForeignKey(
        'MusicDetails', on_delete=models.CASCADE, verbose_name='音乐')
    rating_score = models.IntegerField(default=5, verbose_name='评分')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评分时间')

    class Meta:
        verbose_name = '音乐评分'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.user.username
